package com.softhis.rekrutacja.client.presenter;

import com.allen_sauer.gwt.log.client.Log;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.softhis.rekrutacja.client.MainEventBus;
import com.softhis.rekrutacja.client.commons.ClientSession;
import com.softhis.rekrutacja.client.view.UserFormView;
import com.softhis.rekrutacja.client.view.widgets.interfaces.IButton;
import com.softhis.rekrutacja.client.view.widgets.interfaces.IDateField;
import com.softhis.rekrutacja.client.view.widgets.interfaces.ITextField;
import com.softhis.rekrutacja.shared.dto.UserDTO;
import com.softhis.rekrutacja.shared.services.UserService.SaveUserCode;
import com.softhis.rekrutacja.shared.services.UserServiceAsync;

@Presenter(view = UserFormView.class)
public class UserFormPresenter extends BasePresenter<UserFormPresenter.UserFormViewInterface, MainEventBus> {

	@Inject
	private UserServiceAsync userService = null;

	/**
	 * Create a remote user service.
	 */
	public void setUserService(UserServiceAsync userService) {
		this.userService = userService;
	}

	private ClickHandler cancelNewUserCH = new ClickHandler() {

		public void onClick(ClickEvent event) {
			view.clearForm();
		}
	};

	private ClickHandler cancelProfileCH = new ClickHandler() {

		public void onClick(ClickEvent event) {
			view.fillForm(ClientSession.getLoggedUser());
		}
	};

	private ClickHandler saveNewUserCH = new ClickHandler() {

		public void onClick(ClickEvent event) {

			userService.saveUser(view.getUserData(), new AsyncCallback<SaveUserCode>() {
				public void onFailure(Throwable caught) {
					Log.error("[USER FORM PRESENTER] create new user data fail ", caught);
					MessageBox.alert("Komunikat", "Utracono połącznie z serwerem, spróbuj ponownie później.", null);
				}

				public void onSuccess(SaveUserCode result) {
					switch (result) {
					case LOGIN_IN_USE:
						MessageBox.alert("Komunikat", "Wybrany login jest już używany przez innego użytkownika.", null);
						break;
					case OK:
						MessageBox.alert("Sukces", "Dodano nowe konto.", null);
						view.clearForm();
						break;
					default:
						break;
					}
				}
			});

		}
	};
	
	private ClickHandler saveProfileCH = new ClickHandler() {

		public void onClick(ClickEvent event) {

			UserDTO tempUser = view.getUserData();
			tempUser.setUserId(ClientSession.getLoggedUser().getUserId());
			
			userService.updateUser(tempUser, new AsyncCallback<SaveUserCode>() {
				public void onFailure(Throwable caught) {
					Log.error("[USER FORM PRESENTER] update user data fail ", caught);
					MessageBox.alert("Komunikat", "Utracono połącznie z serwerem, spróbuj ponownie później.", null);
				}

				public void onSuccess(SaveUserCode result) {
					switch (result) {
					case LOGIN_IN_USE:
						MessageBox.alert("Komunikat", "Wybrany login jest już używany przez innego użytkownika.", null);
						break;
					case OK:
						
						UserDTO tempUser = view.getUserData();
						tempUser.setUserId(ClientSession.getLoggedUser().getUserId());
						ClientSession.setLoggedUser(tempUser);
						MessageBox.alert("Sukces", "Zmieniono dane użytkownika", null);
						break;
					default:
						break;
					}
				}
			});

		}
	};
	
	// click handler remover
	HandlerRegistration cancelRegistration;
	HandlerRegistration saveRegistration;

	@Override
	public void bind() {

		this.cancelRegistration = view.getCancelButton().addClickHandler(this.cancelNewUserCH);
		this.saveRegistration = view.getSaveButton().addClickHandler(this.saveNewUserCH);
	}

	// Event Bus methods 

	public void onOpenProfile() {
		view.clearForm();
		this.cancelRegistration.removeHandler();
		this.cancelRegistration = view.getCancelButton().addClickHandler(this.cancelProfileCH);
		this.saveRegistration.removeHandler();
		this.saveRegistration = view.getSaveButton().addClickHandler(this.saveProfileCH);
		view.getCancelButton().setText("Anuluj");
		view.fillForm(ClientSession.getLoggedUser());
		eventBus.setContent((Widget) view);
	}

	public void onOpenNewUser() {
		view.clearForm();
		this.cancelRegistration.removeHandler();
		this.cancelRegistration = view.getCancelButton().addClickHandler(this.cancelNewUserCH);
		this.saveRegistration.removeHandler();
		this.saveRegistration = view.getSaveButton().addClickHandler(this.saveNewUserCH);
		view.getCancelButton().setText("Wyczyść");
		eventBus.setContent((Widget) view);
	}

	/**
	 * @author Sasak
	 * 
	 *         user form view interface
	 */
	public interface UserFormViewInterface {

		public void clearForm();

		public UserDTO getUserData();

		public void fillForm(UserDTO user);

		public IButton getSaveButton();

		public IButton getCancelButton();

		public ITextField getLoginField();

		public ITextField getPasswordField();

		public ITextField getNameField();

		public ITextField getLastNameField();

		public IDateField getBirthDateField();

	}

}
